<?php
/*
 * Created on Jun 14, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class NewsPage extends Page {
	function getCMSFields($controller = null) {
		$fields = parent::getCMSFields();
		$fields->removeFieldFromTab("Root.Content.Main","Content");
		return $fields;
	}
}
class NewsPage_Controller extends Page_Controller {
	protected $numPerPage = "11";
	function init() {
		parent :: init();
		Requirements :: css('news/css/news.css', 'screen');
	}
	
	public function getNewCategories(){
		$locale=$this->current_lang();
		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
			"*",
			"Name_$locale as Title",
		);
		$sqlQuery->from = array("NewsCategory",);
		$sqlQuery->where = array("Status=1");
		$sqlQuery->orderby("Position");
		$rawSQL = $sqlQuery->sql();
		$result = $sqlQuery->execute();
		$dataObject = new DataObjectSet();
		$URLSeg = Lib::getURLSegmentByClassName('NewsPage');
		foreach($result as $row) {
			$row['Link']=$URLSeg."/cat/".$row['ID'];
			$dataObject->push(new ArrayData($row));
		}
		if($dataObject)
		$dataObject->parseQueryLimit($sqlQuery);
		//Debug::dump($dataObject);
		return $dataObject;
	}
	public function getNews()
	{
		if (!isset ($_GET['start']) || !is_numeric($_GET['start']) || (int) $_GET['start'] < 1)
		$_GET['start'] = 0;
		$SQL_start = (int) $_GET['start'];
		$locale=$this->current_lang();
		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
			"News.ID as ID",
	     	"News.Title_$locale as Title",
			"News.Summary_$locale as Summary",
			"News.Content_$locale as Content", 
			"DATE_FORMAT(News.Created,'%d.%m.%Y') as PublishDate",
			"News.ImageID as ImageID",
		//"File.FileName as Image",
		);
		$sqlQuery->from = array("News","LEFT JOIN File ON News.ImageID = File.ID");
		$sqlQuery->where = array("Status=1");
		$sqlQuery->orderby("News.PublishDate DESC, News.Created DESC");
		$sqlQuery->limit = "$SQL_start,4";
		// get the raw SQL
		$rawSQL = $sqlQuery->sql();
		$result = $sqlQuery->execute();
		$dataObject = new DataObjectSet();
		$URLSeg = Lib::getURLSegmentByClassName('NewsPage');
		foreach($result as $row) {
			$row['Link']=$URLSeg."/show/".$row['ID'];
			$row['Image']= Lib::getImages($row['ImageID']);
			$dataObject->push(new ArrayData($row));
		}
		if($dataObject)
		$dataObject->parseQueryLimit($sqlQuery);
		return $dataObject;
	}
	public function getTopNews()
	{
		if (!isset ($_GET['start']) || !is_numeric($_GET['start']) || (int) $_GET['start'] < 1)
		$_GET['start'] = 0;
		$SQL_start = (int) $_GET['start'];
		$locale=$this->current_lang();
		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
			"News.ID as ID",
	     	"News.Title_$locale as Title",
			"News.Summary_$locale as Summary",
			"News.Content_$locale as Content", 
			"DATE_FORMAT(News.Created,'%d.%m.%Y') as PublishDate",
			"News.ImageID as ImageID",
			//"File.FileName as Image",
		);
		$sqlQuery->from = array("News");
		$sqlQuery->where = array("Status=1");
		$sqlQuery->orderby("News.PublishDate DESC, News.Created DESC");
		$sqlQuery->limit = "0,10";
		// get the raw SQL
		$rawSQL = $sqlQuery->sql();
		$result = $sqlQuery->execute();
		$dataObject = new DataObjectSet();
		$URLSeg = Lib::getURLSegmentByClassName('NewsPage');
		foreach($result as $row) {
			$row['Link']=$URLSeg."/show/".$row['ID'];
			$row['Image']= Lib::getImages($row['ImageID']);
			$dataObject->push(new ArrayData($row));
		}
		if($dataObject)
		$dataObject->parseQueryLimit($sqlQuery);
		return $dataObject;
	}
	public function getNewsDetail()
	{
		$params = Controller::curr()->getURLParams();
		$id = (isset($params)) ? $params['ID'] : false;
		$_GET['start'] = 0;
		$SQL_start = (int) $_GET['start'];
		$Limit = $this->numPerPage;
		$locale=$this->current_lang();
		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
		"News.ID as ID",
     	"News.Title_$locale as Title",
		"News.Summary_$locale as Summary",
		"News.Content_$locale as Content", 
		"DATE_FORMAT(News.Created,'%d.%m.%Y') as PublishDate",
		"File.FileName as Image",
		);
		$sqlQuery->from = array("News","LEFT JOIN File ON News.ImageID = File.ID");
		$sqlQuery->where = array("News.Status=1 and News.ID=$id");
		// get the raw SQL
		$rawSQL = $sqlQuery->sql();
		$result = $sqlQuery->execute();
		$dataObject = new DataObjectSet();
		$URLSeg = Lib::getURLSegmentByClassName('NewsPage');
		foreach($result as $row) {
			$row['Link']= $URLSeg ."/show/".$row['ID'];
			$dataObject->push(new ArrayData($row));
		}
		if($dataObject)
		$dataObject->parseQueryLimit($sqlQuery);
		return $dataObject;
	}
}
?>